<template>
    <div class="my-projectForm">
        <v-dialog
            :title="isAdd ? '新增' : '编辑'"
            :show.sync="dialogVisible"
            width="1200px"
            @beforeClose="beforeClose"
            :close-on-click-modal="false">
            <div class="card-box">
                <div class="card-title">工程信息</div>
                <el-form ref="information" :model="projectForm.information" class="form-wrap" size="small" :rules="information_rules" label-position="top">
                    <el-form-item label="工程名称" prop="projectName">
                        <el-input v-model="projectForm.information.projectName" maxlength="50" placeholder="请输入工程名称"></el-input>
                    </el-form-item>
                    <el-form-item label="工程地址" prop="projectAddress">
                        <el-input v-model="projectForm.information.projectAddress" maxlength="50" placeholder="请输入工程地址"></el-input>
                    </el-form-item>
                    <el-form-item label="工程类型" prop="projectType">
                        <el-select v-model="projectForm.information.projectType" placeholder="请选择工程类型">
                            <el-option
                                v-for="item in projectTypes"
                                :key="item.label"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="建设内容" class="full" prop="projectContent">
                        <el-input v-model="projectForm.information.projectContent" maxlength="300" placeholder="请输入建设内容"></el-input>
                    </el-form-item>
                    <el-form-item label="建设规模" class="full" prop="projectScale">
                        <el-input v-model="projectForm.information.projectScale" maxlength="300" placeholder="请输入建设规模"></el-input>
                    </el-form-item>
                    <el-form-item label="工程建设工期" prop="projectStart">
                        <el-date-picker type="daterange"
                                        start-placeholder="开始日期"
                                        end-placeholder="结束日期"
                                        v-model.trim="projectForm.information.projectStart"
                                        format="yyyy-MM-dd"
                                        valueFormat="yyyy-MM-dd">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="资金来源" prop="projectSource">
                        <el-input v-model="projectForm.information.projectSource" maxlength="50" placeholder="请输入资金来源"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" class="full" prop="remarks">
                        <el-input v-model="projectForm.information.remarks" maxlength="500" placeholder="请输入备注"></el-input>
                    </el-form-item>
                </el-form>
                <div class="card-title">代建单位信息</div>
                <el-form ref="agentUnit" :model="projectForm.agentUnit" class="form-wrap" size="small" :rules="agentUnit_rules" label-position="top">
                    <el-form-item label="代建单位" prop="agentInfo">
                        <el-input v-model="projectForm.agentUnit.agentInfo" maxlength="50" placeholder="请输入代建单位"></el-input>
                    </el-form-item>
                    <el-form-item label="单位联系人" prop="agentContact">
                        <el-input v-model="projectForm.agentUnit.agentContact" maxlength="30" placeholder="请输入单位联系人"></el-input>
                    </el-form-item>
                    <el-form-item label="联系电话" prop="agentPhone">
                        <el-input v-model="projectForm.agentUnit.agentPhone" maxlength="20" placeholder="请输入联系电话"></el-input>
                    </el-form-item>
                </el-form>
                <div class="card-title">代建单位中标通知书信息</div>
                <el-form ref="winningBid" :model="projectForm.winningBid" class="form-wrap" size="small" :rules="winningBid_rules" label-position="top">
                    <el-form-item label="中标通知书文号" prop="bidDocument">
                        <el-input v-model="projectForm.winningBid.bidDocument" maxlength="50" placeholder="请输入中标通知书文号"></el-input>
                    </el-form-item>
                    <el-form-item label="金额(元)" prop="bidAmount">
                        <el-input v-model="projectForm.winningBid.bidAmount" maxlength="20" placeholder="请输入金额"></el-input>
                    </el-form-item>
                    <el-form-item label="审批日期" prop="bidApprove">
                        <el-date-picker
                            v-model="projectForm.winningBid.bidApprove"
                            type="date"
                            value-format="yyyy-MM-dd"
                            format="yyyy-MM-dd"
                            placeholder="请选择审批日期"
                        ></el-date-picker>
                    </el-form-item>
                </el-form>
                <div class="card-title">附件</div>
                <div class="handling-opinions">
                    <v-upload
                        ref="registrationUpload"
                        :busId="projectForm.information.projectId"
                        modelType="registration"
                        @onSuccess="onSuccess"
                        isShowFileList
                        :isView="false"
                        multiple
                        v-model="fileList"/>
                </div>
            </div>
            <span slot="foot">
            <el-button @click="handleCancel">取 消</el-button>
            <el-button v-throttle="[commit]">保 存</el-button>
          </span>
        </v-dialog>
    </div>
</template>

<script>
    import BaseLoad from '@/assets/vue-mixin/BaseLoad'
    import { doubleValidate,validatePhone} from '~/utils/validate.js'
    import {save, update, get, validateBidDocument, validateName} from '../api/registration'
    export default {
        name: "edit-data",
        created() {
            this.projectTypes = this.getDict('epc_project_type')
        },
        mixins: [BaseLoad],
        data() {
            let validateBidDo = (rule, value, callback) => {
                if (!value) {
                    callback()
                }
                this.validateOnly(value, callback)
            };
            let validateProjectName = (rule, value, callback) => {
                if (!value) {
                    callback()
                }
                this.validateNameOnly(value, callback)
            };
            return {
                dialogVisible: false,
                isAdd: true,
                projectForm: {
                    information: {
                        projectId: '',
                        projectName: '',
                        projectAddress: '',
                        projectType: '',
                        projectContent: '',
                        projectScale: '',
                        projectStart: '',
                        projectEnd: '',
                        projectSource: '',
                        remarks: ''
                    },
                    agentUnit: {
                        agentInfo: '',
                        agentContact: '',
                        agentPhone: ''
                    },
                    winningBid: {
                        bidDocument: '',
                        bidAmount: '',
                        bidApprove: ''
                    }
                },
                registrationForm: {},
                projectTypes: [],
                fileList: [],
                information_rules: {
                    projectName: [{required: true, message: '请输入工程名称', trigger: 'blur'},
                        { validator: validateProjectName, trigger: "blur"}],
                    projectAddress: [{required: true, message: '请输入工程地址', trigger: 'blur'}],
                    projectScale: [{required: true, message: '请输入建设规模', trigger: 'blur'}],
                    projectContent: [{required: true, message: '请输入建设内容', trigger: 'blur'}],
                    projectType: [{required: true, message: '请选择工程类型', trigger: 'change'}]
                },
                agentUnit_rules: {
                    agentPhone: [{required: false, message: '请输入联系电话', trigger: 'blur'},
                        { validator: validatePhone, trigger: "blur"}]
                },
                winningBid_rules: {
                    bidAmount: [{required: false, message: '请输入金额', trigger: 'blur'},
                        { validator: doubleValidate, trigger: "blur"}],
                    bidDocument: [{required: false, message: '请输入中标通知书文号', trigger: 'blur'},
                        { validator: validateBidDo, trigger: "blur"}],
                }
            }
        },
        methods: {
            //打开弹框
            show(item) {
                this.dialogVisible = true
                if (item) {
                    this.isAdd = false
                    this.projectForm.information.projectId = item
                    get(item).then(res => {
                        if (res.data.code === 0) {
                            this.projectForm.information = res.data.data.epcProject
                            this.projectForm.agentUnit = res.data.data.epcProject
                            this.projectForm.winningBid = res.data.data.epcProject
                            this.projectForm.information.projectStart = [this.projectForm.information.projectStart,this.projectForm.information.projectEnd]
                        }
                    })
                }
            },
            //关闭弹框
            beforeClose(done) {
                this.$emit('beforeClose')
            },
            //取消
            handleCancel() {
                this.$emit('handleClose')
                this.dialogVisible = false
            },
            //保存
            commit() {
                const information = new Promise((resolve, reject) => {
                    this.$refs['information'].validate((valid) => {
                        if (valid) resolve();
                    })
                })
                const agentUnit = new Promise((resolve, reject) => {
                    this.$refs['agentUnit'].validate((valid) => {
                        if (valid) resolve();
                    })
                })
                const winningBid = new Promise((resolve, reject) => {
                    this.$refs['winningBid'].validate((valid) => {
                        if (valid) resolve();
                    })
                })
                Promise.all([information, agentUnit, winningBid]).then(() => {
                    this.registrationForm = {
                        ...this.projectForm.information,
                        ...this.projectForm.agentUnit,
                        ...this.projectForm.winningBid}
                    if (this.registrationForm.projectStart) {
                        const [projectStart,projectEnd] = this.registrationForm.projectStart
                        this.registrationForm.projectStart = projectStart
                        this.registrationForm.projectEnd = projectEnd
                    }
                    this.$refs.registrationUpload.submit();
                })
            },
            //文件上传
            onSuccess(response, file, fileList){
                //文件上传成功，开始保存表单
                if (fileList !== undefined && fileList.length > 0) {
                    if (response.data) {
                        this.registrationForm.projectId = response.data
                        this.saveFormData(response.data)
                    } else {
                        this.err('文件上传失败')
                    }
                } else {
                    this.saveFormData(response.data)
                }
            },
            saveFormData () {
                this.loadAction('数据加载中')
                if (this.isAdd) {
                    save(this.registrationForm).then((rep) => {
                        this.loadClose()
                        this.$message.success("保存成功");
                        this.dialogVisible = false
                        this.$emit('resetList')
                    }).catch((e) => {
                        this.loadClose()
                        this.$message.error("保存失败");
                    })
                } else {
                    update(this.registrationForm).then((rep) => {
                        this.loadClose()
                        this.$message.success("修改成功");
                        this.dialogVisible = false
                        this.$emit('resetList')
                    }).catch((e) => {
                        this.loadClose()
                        this.$message.error("修改失败");
                    });
                }
            },
            //验证中标通知书文号是否存在
            validateOnly (value, callback) {
                validateBidDocument({ "projectId": this.projectForm.information.projectId, "bidDocument": value }).then(rep => {
                    let vr = rep.data.data
                    if (vr === 0) {
                        return callback();
                    } else {
                        return callback(new Error("中标通知书文号不能重复"));
                    }
                })
            },
            //验证工程名称是否存在
            validateNameOnly (value, callback) {
                validateName({ "projectId": this.projectForm.information.projectId, "projectName": value }).then(rep => {
                    let vr = rep.data.data
                    if (vr === 0) {
                        return callback();
                    } else {
                        return callback(new Error("工程名称不能重复"));
                    }
                })
            },
        }

    }
</script>

<style scoped lang='scss'>
    @include formWrap(3);
    .handling-opinions {
        padding: 20px;
    }
</style>
